﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
/// <summary>
/// 分为三个类. MainWindow: 窗口界面; 网络: 与网络的交互; 信息: 学生学号等信息
/// </summary>
namespace PC
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<数据.用户信息> 学生信息 = new List<数据.用户信息>();
        //数据.用户信息[] s = new 数据.用户信息[] { new 数据.用户信息 { 学号 = "12", 密码 = "13" }, new 数据.用户信息 { 学号 = "123", 密码 = "33" } };

        public MainWindow()
        {

            InitializeComponent();
            //增加用户信息列表
            //学生信息.Add(new 数据.用户信息 { 学号 = "1301110110", 密码 = "Oudanyi6456" });
            //学生信息[1] = new 数据.用户信息 { 学号 = "gcuspea", 密码 = "phyfsc508" };
            string 地址 = Environment.CurrentDirectory + "\\config.ini", 文本流;
            try
            {
                FileStream 文件 = new FileStream(地址, FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(文件);
                while ((文本流 = sr.ReadLine()) != null)
                {
                    学生信息.Add(new 数据.用户信息 { 学号 = 文本流, 密码 = sr.ReadLine() });
                }
                sr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            label.MouseDoubleClick += (sender, e) => System.Diagnostics.Process.Start(地址);
            comboBox.ItemsSource = 学生信息;
            comboBox.DisplayMemberPath = "学号";
            if (学生信息.Count != 0) comboBox.SelectedValue = 学生信息[0];//默认

            系统托盘();
        }

        System.Windows.Forms.NotifyIcon 图标 = new System.Windows.Forms.NotifyIcon();
        private void 系统托盘()
        {


            //图标.Icon = new System.Drawing.Icon(@"E:\Code\Visual Studio\网关\PC\Resources\1.ico");
            //图标.Icon = System.Drawing.SystemIcons.Exclamation;//小图标的样子
            图标.Icon = Properties.Resources.免费;


            图标.Visible = true;
            //添加托盘图标的右键菜单
            图标.ContextMenu = new System.Windows.Forms.ContextMenu();
            图标.ContextMenu.MenuItems.Add("连接免费地址", 连接);
            图标.ContextMenu.MenuItems.Add("连接收费地址", 连接);
            图标.ContextMenu.MenuItems.Add("断开连接", 连接);
            图标.ContextMenu.MenuItems.Add("断开所有连接", 连接);
            for (int i = 0; i < 4; i++)
            {
                图标.ContextMenu.MenuItems[i].Tag = i;
            }

            图标.ContextMenu.MenuItems.Add("退出", 退出);

            图标.MouseClick += delegate (object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    if (WindowState == WindowState.Normal)
                    {
                        WindowState = WindowState.Minimized;
                    }
                    else
                    {
                        WindowState = WindowState.Normal;
                        Activate();
                    }

                }

            };

            //lambda表达式, 窗口关闭时notifyicon关闭
            Closed += 退出;
            StateChanged += (sender, e) =>
            {
                //断开本地连接, 不需要吧?
                //web.连接(连接类型[1]);
                //if (WindowState == WindowState.Minimized) ShowInTaskbar = false;
                //else if (WindowState == WindowState.Normal) ShowInTaskbar = true;
                ShowInTaskbar = WindowState == WindowState.Normal;
            };

        }


        private void 退出(object sender, EventArgs args)
        {
            if (图标 != null)
            {
                //图标.Visible = false;
                图标.Dispose();
                图标 = null;
            }
            web.连接(连接类型[2]);//断开连接
            Close();
        }

        网络 web = new 网络();



        static string[] 连接类型 = new string[] { "ipgwopen", "ipgwopenall", "ipgwclose", "ipgwcloseall" };//连接, 收费链接, 断开连接, 断开所有连接
        Button[] IP按钮 = new Button[3];
        //确定免费/收费地址
        short 连接Tag;
        /// <summary>
        /// 利用Button内的Tag确定(断开)连接的类型
        /// </summary>
        /// <param name="sender">点击的Button种类</param>
        /// <param name="e"></param>
        private void 连接(object sender, EventArgs e)
        {
            try
            {
                连接Tag = Convert.ToInt16(((FrameworkElement)sender).Tag);//在主界面上点击按钮
                if ((bool)radioButton1.IsChecked && 连接Tag == 0) 连接Tag = 1;//收费
            }
            catch (Exception)
            {
                连接Tag = Convert.ToInt16(((System.Windows.Forms.Menu)sender).Tag);//点击托盘菜单
                if (连接Tag == 1)
                    radioButton1.IsChecked = true;
                else if (连接Tag == 0)
                    radioButton.IsChecked = true;
            }


            //清除文本信息
            textBlock.Inlines.Clear();
            foreach (Button item in IP按钮)
            {
                grid.Children.Remove(item);
            }
            //确定学号和密码
            web.学生 = (数据.用户信息)comboBox.SelectedItem;



            string[] Content = web.连接(连接类型[连接Tag]);
            判断(Content);
        }


        /// <summary>
        /// 通过点击Button断开指定的IP地址网关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 指定连接(object sender, RoutedEventArgs e)
        {
            textBlock.Inlines.Clear();
            foreach (Button item in IP按钮)
            {
                grid.Children.Remove(item);
            }
            string[] Content = web.断开指定连接(((Button)sender).Tag.ToString());
            判断(Content);
        }
        private void 判断(string[] Content)
        {
            if (Content[0] == "错误")
            {
                textBlock.Inlines.Add(new Run(Content[1]));
                WindowState = WindowState.Normal;
                return;
            }
            if (Content[0].Contains("YES"))//(断开)连接成功, 显示信息
            {
                for (int i = 1; i < Content.Count(); i++)
                {
                    textBlock.Inlines.Add(new Run(Content[i]));
                    textBlock.Inlines.Add(new LineBreak());
                    //if (连接Tag == 3) 图标.Icon = new System.Drawing.Icon(@"E:\Code\Visual Studio\网关\PC\Resources\2.ico");//收费连接更改图标
                    //else 图标.Icon = new System.Drawing.Icon(@"E:\Code\Visual Studio\网关\PC\Resources\1.ico");
                }
                if (连接Tag == 1) 图标.Icon = Properties.Resources.收费;//收费图标
                else 图标.Icon = Properties.Resources.免费;
            }
            else//连接失败. 假如还有别的后续版本, 将这个部分归类到"错误"中
            {
                int IP数量 = Content.Length / 2;

                WindowState = WindowState.Normal;
                for (int i = 0; i < IP数量; i++)//新建选IP地址断开的按钮
                {

                    IP按钮[i] = new Button();

                    IP按钮[i].Content = "断开IP" + (i + 1);
                    IP按钮[i].HorizontalAlignment = HorizontalAlignment.Center;
                    IP按钮[i].Width = 70;
                    IP按钮[i].Height = 27;
                    IP按钮[i].VerticalAlignment = VerticalAlignment.Bottom;
                    IP按钮[i].Tag = Content[2 * i];
                    IP按钮[i].Margin = new Thickness((300 - 50 * IP数量) * (i - (IP数量 - 1) / 2), 0, 0, 52);
                    IP按钮[i].Click += 指定连接;
                    grid.Children.Add(IP按钮[i]);
                }

                textBlock.Inlines.Add(new Run("已打开多个网络连接, 请断开指定连接"));
                for (int i = 0; i < IP数量; i++)
                {
                    textBlock.Inlines.Add(new LineBreak());
                    textBlock.Inlines.Add(new Run("IP" + (i + 1) + ":" + Content[2 * i] + "\n连接时间:" + Content[2 * i + 1] + "\n"));
                }
                WindowState = WindowState.Normal;//恢复正常页面等待处理
            }
        }


    }



}
